/**
 * 
 */
package com.bestbuy.daoimple;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bestbuy.dao.BillDAO;
import com.bestbuy.pojo.Bill;
import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;

/**
 * @author ThanhNgo
 *
 */
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class BillDAOImpl implements BillDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#getAllBill()
	 */
	public List<Bill> getAllBill() {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Bill";
		Query query = session.createQuery(hql);
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#getAllBill(java.lang.String)
	 */
	public List<Bill> getAllBill(String page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		int temp = Integer.parseInt(page)-1;
		List<Bill> bills ;
		String sql = "from Bill";
		Query query = session.createQuery(sql);
		query.setFirstResult(temp*5);
		query.setMaxResults(5);
		bills = (List<Bill>) query.list();
		return bills;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#getBillIsNotPaid(java.lang.String)
	 */
	public List<Bill> getBillIsNotPaid(String isPaid, String page) {
		Session session = sessionFactory.getCurrentSession();
		int temp = Integer.parseInt(page)-1;
		List<Bill> bills ;
		String sql = "from Bill where ispaid = ?";
		Query query = session.createQuery(sql);
		query.setString(0, isPaid);
		query.setFirstResult(temp*5);
		query.setMaxResults(5);
		bills = (List<Bill>) query.list();
		return bills;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#checkPaid(java.util.List)
	 */
	public void checkPaid(List<Bill> listBill) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#countAllBill()
	 */
	public int countAllBill() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String sql = "select count(*) from Bill";
		Query query = session.createQuery(sql);
		int count = ((Long) query.uniqueResult()).intValue();
		int page = count/5;
		int du = count%5;
		if(du>0)
		{
			page = page+1;
		}
		
		return page;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#countAllBill(java.lang.String)
	 */
	public int countAllBill(String isPaid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String sql = "select count(*) from Bill where ispaid = ?";
		Query query = session.createQuery(sql);
		query.setString(0, isPaid);
		int count = ((Long) query.uniqueResult()).intValue();
		int page = count/5;
		int du = count%5;
		if(du>0)
		{
			page = page+1;
		}
		
		return page;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#updateBill(java.lang.String)
	 */
	public void updateBill(String bill) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#updateBill(com.bestbuy.pojo.Bill)
	 */
	public void updateBill(Bill bill) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.update(bill);
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.BillDAO#getBill(java.lang.String)
	 */
	public Bill getBill(String idBill) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Bill where idbill = ?";
		Query query = session.createQuery(hql);
		query.setString(0, idBill);
		return (Bill)query.uniqueResult();
	}

}
